import Router, { RouteConfig } from 'vue-router'
// @ts-ignore
import Vue from 'vue'
import App from '~/App.vue'
import Login from '~/pages/login/index.vue'
import Home from '~/pages/home/index.vue'
import Boss from '~/pages/home/boss/index.vue'
import CustomerService from '~/pages/home/customer-service/index.vue'
import ChannelCommissioner from '~/pages/home/channel-commissioner/index.vue'
import Platform from '~/pages/platform/index.vue'
import System from '~/pages/system/index.vue'

import Message from '~/pages/message/index.vue'
import MessageBill from '~/pages/message/bill/index.vue'
import MessageTrans from '~/pages/message/trans/index.vue'
import MessageSystem from '~/pages/message/system/index.vue'

import RankBoss from '~/pages/home/rank-boss/index.vue'
import RankChannel from '~/pages/home/rank-channel/index.vue'
const routes: RouteConfig[] = [
  {
    path: '/',
    name: 'app',
    component: App,
    // redirect: '/login',
    children: [
      {
        path: 'home',
        component: Home,
        name: '主页',
        children: [
          {
            path: 'boss',
            component: Boss
          },
          {
            path: 'customer-service',
            component: CustomerService
          },
          {
            path: 'channel-commissioner',
            component: ChannelCommissioner
          },
          {
            path: 'rank-boss/:id',
            component: RankBoss,
            name: '排行榜/大掌柜',
            meta: '/home/rank-boss'
          },
          {
            path: 'rank-channel/:id',
            component: RankChannel,
            name: '排行榜/渠道专员',
            meta: '/home/rank-channel'
          }
        ]
      },
      {
        name: '消息提醒',
        path: 'message',
        redirect: '/message/bill',
        component: Message,
        children: [
          {
            name: '票据提醒',
            path: 'bill',
            component: MessageBill,
            meta: '/message/bill'
          },
          {
            name: '交易提醒',
            path: 'trans',
            component: MessageTrans,
            meta: '/message/trans'
          },
          {
            name: '系统提醒',
            path: 'system',
            component: MessageSystem,
            meta: '/message/system'
          }
        ]
      },
      {
        name: '平台管理',
        path: 'platform',
        component: Platform,
        children: [
          {
            path: 'member',
            name: '会员管理',
            component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/member/index.vue'),
            redirect: '/platform/member/list',
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/member/list/index.vue'),
                meta: {
                  activeRoot: '/platform/member'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/member/detail/index.vue'),
                meta: {
                  activeRoot: '/platform/member'
                }
              }
            ]
          },
          {
            path: 'salesmen',
            name: '业务员管理',
            component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/salesmen/index.vue'),
            redirect: '/platform/salesmen/list',
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/salesmen/list/index.vue'),
                meta: {
                  activeRoot: '/platform/salesmen'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/salesmen/detail/index.vue'),
                meta: {
                  activeRoot: '/platform/salesmen'
                }
              }
            ]
          },
          {
            path: 'channel-commissioner',
            name: '渠道专员管理',
            component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/channel-commissioner/index.vue'),
            redirect: '/platform/channel-commissioner/list',
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/channel-commissioner/list/index.vue'),
                meta: {
                  activeRoot: '/platform/channel-commissioner'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/channel-commissioner/detail/index.vue'),
                meta: {
                  activeRoot: '/platform/channel-commissioner'
                }
              }
            ]
          },
          {
            path: 'channel-provider',
            name: '渠道商管理',
            component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/channel-provider/index.vue'),
            redirect: '/platform/channel-provider/list',
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/channel-provider/list/index.vue'),
                meta: {
                  activeRoot: '/platform/channel-provider'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "platform" */ '~/pages/platform/channel-provider/detail/index.vue'),
                meta: {
                  activeRoot: '/platform/channel-provider'
                }
              }
            ]
          }
        ]
      },
      {
        path: 'transaction',
        component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/index.vue'),
        name: '交易管理',
        children: [
          {
            path: 'order',
            name: '订单管理',
            redirect: '/transaction/order/list',
            component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/order/index.vue'),
            meta: {
              title: '票据融资',
              activeRoot: '/transaction/order'
            },
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/order/list/index.vue'),
                meta: {
                  activeRoot: '/transaction/order'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/order/detail/index.vue'),
                meta: {
                  activeRoot: '/transaction/order'
                }
              }
            ]
          },
          {
            path: 'offer',
            name: '询价管理',
            component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/offer/index.vue'),
            redirect: '/transaction/offer/list',
            meta: {
              title: '票据融资',
              activeRoot: '/transaction/offer'
            },
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/offer/list/index.vue'),
                meta: {
                  activeRoot: '/transaction/offer'
                }
              },
              {
                path: 'adjust-price/:id',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/offer/adjust-price/index.vue'),
                meta: {
                  activeRoot: '/transaction/offer'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/offer/detail/index.vue'),
                meta: {
                  activeRoot: '/transaction/offer'
                }
              }
            ]
          },
          {
            path: 'inventory',
            redirect: '/transaction/inventory/list',
            component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/inventory/index.vue'),
            name: '票据库存',
            children: [
              {
                path: 'list',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/inventory/list/index.vue'),
                meta: {
                  activeRoot: '/transaction/inventory'
                }
              },
              {
                path: 'detail/:id',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/inventory/detail/index.vue'),
                meta: {
                  activeRoot: '/transaction/inventory'
                }
              },
              {
                path: 'audit/:id',
                component: () => import(/* webpackChunkName: "transaction" */ '~/pages/transaction/inventory/audit/index.vue'),
                meta: {
                  activeRoot: '/transaction/inventory'
                }
              }
            ]
          }
        ]
      },
      {
        path: 'system',
        name: '系统管理',
        component: System,
        children: [
          {
            path: 'user',
            name: '用户管理',
            component: () => import(/* webpackChunkName: "system" */ '~/pages/system/user/index.vue'),
            meta: {
              activeRoot: '/system/user'
            }
          },
          {
            path: 'role',
            name: '角色管理',
            component: () => import(/* webpackChunkName: "system" */ '~/pages/system/role/index.vue'),
            meta: {
              activeRoot: '/system/role'
            }
          },
          {
            path: 'role-config',
            name: '角色配置',
            component: () => import(/* webpackChunkName: "system" */ '~/pages/system/role-config/index.vue'),
            meta: {
              activeRoot: '/system/role-config'
            }
          },
          {
            path: 'menu',
            name: '菜单管理',
            component: () => import(/* webpackChunkName: "system" */ '~/pages/system/menu/index.vue'),
            meta: {
              activeRoot: '/system/menu'
            }
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: "register" */ '~/pages/register/index.vue'),
    children: [
      {
        path: 'guide',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/guide/index.vue')
      },
      {
        path: 'process-0',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-0/index.vue')
      },
      {
        path: 'process-1',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-1/index.vue')
      },
      {
        path: 'process-2',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-2/index.vue')
      },
      {
        path: 'process-3',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-3/index.vue')
      },
      {
        path: 'process-4',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-4/index.vue')
      },
      {
        path: 'process-5',
        component: () => import(/* webpackChunkName: "register" */ '~/pages/register/process-5/index.vue')
      }
    ]
  }
]
export const router: Router = new Router({
  mode: 'history',
  routes,
  scrollBehavior () {
    return {
      x: 0,
      y: 0
    }
  }
})
let freePage = ['login', 'register']
router.beforeEach((to, form, next) => {
  let token = window.sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    let currentRouter = to.fullPath.split('/')[1]
    if (freePage.indexOf(currentRouter) === -1) {
      Vue.prototype.$notify({
        title: '提示',
        message: '您的登录信息已失效,请重新登录',
        type: 'error'
      })
      next('/login')
    } else {
      next()
    }
  }
})
